package twentyOne;

/**
 * 加入另一个线程
 *
 * @author MSI PC
 * @since 2020-04-28 22:50
 */
public class ThreadJoin {

    public static void main(String[] args) {
        Sleeper
            sleepy = new Sleeper("Sleepy",1500),
            grumpy = new Sleeper("Grumpy",1500);
        Joiner
            dopey = new Joiner("Dopey",sleepy),
            doc = new Joiner("Doc",grumpy);
    }

}


class Sleeper extends Thread{

    private int duration;

    public Sleeper(String name, int sleepTime) {
        super(name);
        this.duration = sleepTime;
        start();
    }

    @Override
    public void run(){
        try{
            sleep(duration);
        }catch (InterruptedException e){
            System.out.println(getName() + " was interrupted. isInterrupted():" + isInterrupted());
            return;
        }
        System.out.println(getName() + " has awakened");
    }
}

class Joiner extends Thread{

    private Sleeper sleeper;
    public Joiner(String name, Sleeper sleeper){
        super(name);
        this.sleeper = sleeper;
        start();
    }

    @Override
    public void run() {
        try{
            sleeper.join();
        }catch (InterruptedException e){
            System.out.println("Interrupted");
        }
        System.out.println(getName() + "join complated");
    }
}

